package com.tbfs.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.opensymphony.xwork2.Action;
import com.tbfs.admin.dao.CompanyDAO;
import com.tbfs.admin.vo.Company;


public class GetJsonAction extends BaseAction{
	private Logger log = Logger.getLogger(getClass());
	private Gson gson = new Gson();
	private static GetJsonAction instance = new GetJsonAction();


	public static GetJsonAction getInstance() {
		return instance;
	}

/*	public String getJson(HttpServletRequest request,
			HttpServletResponse response) {
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		String message = "";
		String type = request.getParameter("type") == null ? "" : request
				.getParameter("type");
		if (type.equalsIgnoreCase("companyinfoList")) {
			message = getCompanyList(ac);
		}
				
		
		
		return message;
	}*/

	
	
	
	

/*	// 得到公司信息列表
	private String getCompanyList(ApplicationContext context) {
		CompanyDAO companyDao = (CompanyDAO) context
				.getBean("companyInfoDao");
		List list = companyDao.queryBySelective(null);
		String message = gson.toJson(list);
		log.info(message);
		return Action.SUCCESS;
	}*/
	
	// 得到公司信息列表
	public  String getCompanyListTree() {
		CompanyDAO companyDao =(CompanyDAO) this.getBean("companyDao");
		List<Company> list = companyDao.queryCompanyInfoTree();
		String message = buildDBTree(list,null);
		HttpServletResponse response = this.getResponse();
		try {
			PrintWriter writer = response.getWriter();
			writer.write(message);
		} catch (IOException e) {
			e.printStackTrace();
		}
		log.info(message);
		return Action.NONE;
	}
	
	/**
	 * 生成动态树
	 * @param dlist
	 * @param supid
	 * @return
	 */
	private String buildDBTree(List<Company> dlist, Integer supid) {
		String tjson = "";
		if (supid == null) {
			for (int i = 0; i < dlist.size(); i++) {
				Company comp = dlist.get(i);
				if (comp.getParentId() == 0) {// 首层部门，写json头（根节点）
					tjson += "[{\"text\": \""+comp.getCompanyName()+"\",\"classes\":\"folder\",\"expanded\":true,\"id\":\""
							+ comp.getCompanyCode() + "\"";
					tjson += buildDBTree(dlist, comp.getId()); // 递归调用:生成子节点json
					tjson += "}]";
				}
			}
		} else { // 子节点
			tjson = "";
			int m = 1;
			int sub = 0;
			for (int i = 0; i < dlist.size(); i++) {
				Company comp = dlist.get(i);
				if (comp.getParentId() != null
						&& comp.getParentId().equals(supid)) {
					if (m == 1) {// 第一个子节点
						tjson += ",\"children\":[{\"text\": \""
								+ comp.getCompanyName()
								+ "\",\"classes\":\"folder\", \"id\":\""
								+ comp.getCompanyCode() + "\"";
						tjson += buildDBTree(dlist, comp.getId());
						tjson += "}";
					} else {// 第二个及其以后的子节点
						tjson += ",{\"text\": \""
								+ comp.getCompanyName()
								+ "\",\"classes\":\"folder\",\"id\":\""
								+ comp.getCompanyCode() + "\"";
						tjson += buildDBTree(dlist, comp.getId());
						tjson += "}";
					}
					m++;
					sub = 1;
				}
			}
			if (sub == 1)
				tjson += "]";
		}
		return tjson;
	}

	public static void main(String[] args) {
		GetJsonAction getJson = GetJsonAction.getInstance();
	}
}
